package com.gzdh.dishrecognition.ui.fragment.sysinfo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.greendao.dao.BmcmEntityDao;
import com.greendao.dao.BmzlEntityDao;
import com.greendao.dao.WeekMenuBakEntityDao;
import com.gzdh.dishrecognition.AppContext;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.entity.BmcmEntity;
import com.gzdh.dishrecognition.entity.BmzlEntity;
import com.gzdh.dishrecognition.entity.WeekMenuBakEntity;
import com.gzdh.dishrecognition.model.WeekMenuBakModel;
import com.gzdh.dishrecognition.ui.adapter.CheckMenuBmcmAdapter;
import com.gzdh.dishrecognition.ui.adapter.HorizontalListViewAdapter;
import com.gzdh.dishrecognition.ui.adapter.LeftBmzlAdapter;
import com.gzdh.dishrecognition.ui.adapter.SysWeekMenuAdapter;
import com.gzdh.dishrecognition.ui.dialog.PaiCaiDialog;
import com.gzdh.dishrecognition.ui.view.HorizontalListView;
import com.gzdh.dishrecognition.util.ProductSUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class CheckMenuFragment extends Fragment {


    private Context mContext;

    private Activity activity;

    public CheckMenuFragment(Activity activity) {
        this.activity = activity;
    }

    private HorizontalListView mHorizontalListView;

    private HorizontalListViewAdapter mHorizontalListViewAdapter;

    private WeekMenuBakEntityDao weekMenuBakEntityDao;

    private BmcmEntityDao bmcmEntityDao;

    private BmzlEntityDao bmzlEntityDao;

    private RecyclerView bmcmRv;

    private RecyclerView zlRv;

    private RecyclerView weekMenuRv;

    private LeftBmzlAdapter leftBmzlAdapter;

    private SysWeekMenuAdapter sysWeekMenuAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_check_menu, container, false);
        mContext = view.getContext();
        mHorizontalListView = (HorizontalListView) view.findViewById(R.id.horizontal_lv);
        bmcmRv = (RecyclerView) view.findViewById(R.id.bmcmRv);
        zlRv = (RecyclerView) view.findViewById(R.id.zlRv);
        weekMenuRv = (RecyclerView) view.findViewById(R.id.weekMenuRv);

        mHorizontalListViewAdapter = new HorizontalListViewAdapter(mContext, mHorizontalListView);
        mHorizontalListView.setAdapter(mHorizontalListViewAdapter);
        weekMenuBakEntityDao = AppContext.getInstance().getDaoSession().getWeekMenuBakEntityDao();
        bmzlEntityDao = AppContext.getInstance().getDaoSession().getBmzlEntityDao();
        bmcmEntityDao = AppContext.getInstance().getDaoSession().getBmcmEntityDao();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        List<String> strings = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            calendar.add(Calendar.DATE, 1);
            strings.add(simpleDateFormat.format(calendar.getTime()));
        }
        leftBmzlAdapter = new LeftBmzlAdapter(mContext);
        sysWeekMenuAdapter = new SysWeekMenuAdapter(mContext);
        mHorizontalListViewAdapter.setWeekList(strings);
        mHorizontalListViewAdapter.notifyDataSetInvalidated();
        mHorizontalListViewAdapter.setmGvOnClick(yday -> {
            List<BmcmEntity> bmcmEntityList = bmcmEntityDao.loadAll();
            CheckMenuBmcmAdapter leftBmcmAdapter = new CheckMenuBmcmAdapter(mContext);
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            bmcmRv.setLayoutManager(layoutManager);
            bmcmRv.setAdapter(leftBmcmAdapter);
            leftBmcmAdapter.setList(bmcmEntityList);
            leftBmcmAdapter.setUserOnClick(bmcmEntity -> showMenu(bmcmEntity, yday));
            sysWeekMenuAdapter.setList(new ArrayList<>());
            leftBmzlAdapter.setList(new ArrayList<>());
        });
        view.findViewById(R.id.paiCaiBut).setOnClickListener(view1 -> {
            PaiCaiDialog paiCaiDialog = new PaiCaiDialog();
            paiCaiDialog.show(getChildFragmentManager(), PaiCaiDialog.class.getSimpleName());
        });
        return view;
    }


    private void showMenu(BmcmEntity bmcmEntity, String yDay) {
        List<BmzlEntity> bmzlEntityList = bmzlEntityDao.loadAll();
        List<WeekMenuBakEntity> weekMenuBakEntityList = weekMenuBakEntityDao.queryBuilder()
                .where(WeekMenuBakEntityDao.Properties.Dinner.eq(bmcmEntity.getBm()))
                .where(WeekMenuBakEntityDao.Properties.Yday.eq(yDay)).list();
        List<WeekMenuBakModel> weekMenuBakModelList = ProductSUtils.getWeekMenuBakModel(bmzlEntityList, weekMenuBakEntityList, bmcmEntity.getBm());
        System.out.println(weekMenuBakModelList.toString());

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        zlRv.setLayoutManager(layoutManager);
        zlRv.setAdapter(leftBmzlAdapter);
        leftBmzlAdapter.setList(weekMenuBakModelList);
        leftBmzlAdapter.setUserOnClick(weekMenuBakModel -> showWeekMenu(weekMenuBakModel));
        sysWeekMenuAdapter.setList(new ArrayList<>());
    }

    private void showWeekMenu(WeekMenuBakModel weekMenuBakModel) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        weekMenuRv.setLayoutManager(layoutManager);
        weekMenuRv.setAdapter(sysWeekMenuAdapter);
        sysWeekMenuAdapter.setList(weekMenuBakModel.getProductList());
    }
}